package com.guo.constellation.starfrag;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.guo.constellation.R;
import com.guo.constellation.bean.StarBean;
import com.guo.constellation.utils.AssetsUtils;

import java.util.List;
import java.util.Map;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * @author glsite.com
 * @version $Rev$
 * @des ${TODO}
 * @updateAuthor $Author$
 * @updateDes ${TODO}
 */
public class StarBaseAdapetr extends BaseAdapter {
    //    一个上下文对象，一个需要的数据对象。
    //然后子自动生成一个构造方法
    Context context;
    List<StarBean.StarinfoBean> mDatas;
    Map<String, Bitmap>logoMap;

    public StarBaseAdapetr(Context context, List<StarBean.StarinfoBean> mDatas) {
        this.context = context;
        this.mDatas = mDatas;
        logoMap = AssetsUtils.getLogoImgMap();
    }

    //返回集合的长度
    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int i) {
        return mDatas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_star_gv,null);
            holder = new ViewHolder(view);
            view.setTag(holder);
        }else {
            holder = (ViewHolder) view.getTag();
        }
        //获取指定位置的数据
        StarBean.StarinfoBean bean = mDatas.get(i);
        holder.tv.setText(bean.getName());
        //获得图片的名称，根据名称在内存中进行查找
        String logoname = bean.getLogoname();
        Bitmap bitmap = logoMap.get(logoname);
        holder.iv.setImageBitmap(bitmap);


        return view;
    }

    //再写一个viewholder对象，这个对象包含mDatas里面的每一个对象
    //该类对item中的控件进行声明和初始化
    class ViewHolder{
        CircleImageView iv;
        TextView tv;
        public ViewHolder(View view){
            iv = view.findViewById(R.id.item_star_iv);
            tv = view.findViewById(R.id.item_star_tv);
        }

    }


}
